// 左叶子节点的定义：节点A的左节点不为空，且左节点的左右子节点都为空，叶子节点 
// 思路1，递归，前序遍历
function sumOfLeftLeaves(root) {
    if (!root) {
        return 0
    }
    let midVal = 0
    if (root.left && !root.left.left && !root.left.right) {
        midVal += root.left.val
    }
    let leftVal = sumOfLeftLeaves(root.left)
    let rightVal = sumOfLeftLeaves(root.right)
    return midVal + leftVal + rightVal
}

// 思路2，迭代
function sumOfLeftLeaves2(root) {
    let stack = [root]
    let sum = 0
    while (stack.length) {
        let node = stack.pop()
        if (node.left && !node.left.left && !node.left.right) {
            sum += node.left.val
        }
        if (node.right) {
            stack.push(node.right)
        }
        if (node.left) {
            stack.push(node.left)
        }
    }
    return sum
}

const root = {
    val: 4,
    left: {
        val: 3,
        left: {
            val: 2,
        },
        right: {
            val: 3.5,
        },
    },
    right: {
        val: 6,
    },
};

console.log(sumOfLeftLeaves(root))
console.log(sumOfLeftLeaves2(root))